﻿var patientId = "";
$(function () {
    patientId = $("#hdnPatientId").val();

    laydate.render({ elem: '#txtEMS_DISEASE_TIME', type: 'datetime' });
    laydate.render({ elem: '#txtHelpTime', type: 'datetime' });
    laydate.render({ elem: '#txtEMS_ARRIVEDOOR_TIME', type: 'datetime' });
    laydate.render({ elem: '#txtContact_Time', type: 'datetime' });

    $('input[name="rdoAssessment_MRS_Bool"]').click(function () {
        if ($("input[name='rdoAssessment_MRS_Bool']:checked").val() == 1) {
            $("#divAssessment_MRS_Bool1").show();
        } else {
            $("#divAssessment_MRS_Bool1").hide();
            $("#txtAssessment_MRS_Score").val("");
        }
    });

    $('input[name="rdoAssessment_GCS_Bool"]').click(function () {
        if ($("input[name='rdoAssessment_GCS_Bool']:checked").val() == 1) {
            $("#divAssessment_GCS_Bool1").show();
        } else {
            $("#divAssessment_GCS_Bool1").hide();
            $("#txtAssessment_MRS_Score").val("");
        }
    });

    $('input[name="rdoAssessment_Swallowing_Bool"]').click(function () {
        if ($("input[name='rdoAssessment_Swallowing_Bool']:checked").val() == 1) {
            $("#divAssessment_Swallowing_Bool1").show();
        } else {
            $("#divAssessment_Swallowing_Bool1").hide();
            $("#txtAssessment_Swallowing_Content").val("");
        }
    });

    //计算BMI
    $("#txtPatient_Height,#txtPatient_Weight").on("blur", function () {
        var float2regex = /^\d+(\.\d{0,2})?$/;
        var height = $("#txtPatient_Height").val();
        var weight = $("#txtPatient_Weight").val();
        if (!float2regex.test(height)) return;
        if (!float2regex.test(weight)) return;
        if (height == 0) return;
        var height2 = parseInt(height) / 100;
        var bmiVal = (parseFloat(weight) / (height2 * height2)).toFixed(2);
        $("#txtPatient_BMI").val(bmiVal);
    });

    $("#btnDel").on("click", function () { delmr(); });
    $("#btnSave").on("click", function () { savemr(); });
    $("#btnHisback").on("click", function () { window.location.href = "/StrokeCenter/MedicalRecords/Index"; });
    $("#btnTimePath").on("click", function () {
        if (IsEmpty(patientId))
            return; window.location.href = "/StrokeCenter/MedicalRecords/Timeline?pid=" + patientId;
    });
    $("#btnTimeLine").on("click", function () {
        if (IsEmpty(patientId)) return;
        var url = "/StrokeCenter/Pathway/Index?id=" + patientId;
        window.parent.top.tab.addTabItem({ tabid: "time" + patientId, text: "时间轴", url: url });
    });

    setRadioCheckboxStyle();
    setValue();
    position();
});

function setRadioCheckboxStyle() {
    $("input[type=radio]").parent().addClass("radio_label");
    $("input[type=radio][disabled!=disabled]").click(function (e) {
        $("input[type=radio][name='" + $(this).attr('name') + "']").parent().removeClass("checked");
        $(this).parent().addClass("checked");
    });
    $("input[type=radio][checked=checked]").parent().addClass("checked");
    if (navigator.userAgent.indexOf("Firefox") > 0) { $(":radio,:checkbox").hide(); }

    $("input[type=checkbox]").parent().addClass("checkbox_label");
    $("input[type=checkbox][disabled!=disabled]").click(function (e) {
        if ($(this).parent().hasClass("checked"))
            $(this).parent().removeClass("checked");
        else
            $(this).parent().addClass("checked");
    });
    $("input[type=checkbox][checked=checked]").parent().addClass("checked");
}

function setValue() {
    if (IsEmpty(patientId)) {
        $.ligerDialog.error("获取数据失败！");
        return;
    }
    var waitting = $.ligerDialog.waitting('加载中，请稍候...');
    $.ajax({
        url: '/StrokeCenter/MedicalRecords/GetPatinet_BaseInfor?patientId=' + patientId + '&rnd=' + getRnd(),
        cache: false,
        dataType: 'json',
        success: function (r) {
            if (r.Result == 1) {
                var datadetail = $.parseJSON(r.Body);
                $.each(FiledArray, function (i, b) {
                    if (b.type == "text") {
                        if (IsEmpty(eval("datadetail." + b.tocode)) || parseInt(eval("datadetail." + b.tocode)) == 0) return true;
                        $('#' + b.code).val(eval("datadetail." + b.tocode));
                        $("input[name='" + b.code + "']").blur();
                    } else if (b.type == "date") {
                        $('#' + b.code).val(obj2Timemm(eval("datadetail." + b.tocode)));
                    } else if (b.type == "radio") {
                        RadioSetVal(b.code, eval("datadetail." + b.tocode), true);
                        $("input[name='" + b.code + "'][value='" + eval("datadetail." + b.tocode) + "']").click();
                    } else if (b.type == "checkbox") {
                        CheckBoxSetVal(b.code, eval("datadetail." + b.tocode), true);
                    }
                });
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        complete: function () {
            waitting.close();
        }
    });
}

function savemr() {
    if (IsEmpty(patientId)) return;
    var data = { PATIENT_ID: patientId };
    $.each(FiledArray, function (i, b) {
        var str = "";
        if (b.type == "text" || b.type == 'date') {
            str = $('#' + b.code).val();
        } else if (b.type == "radio") {
            str = $("input[name='" + b.code + "']:checked").val();
        } else if (b.type == "checkbox") {
            var str = CheckBoxToArrStr(b.code);
        }
        data[b.tocode] = IsEmpty(str) ? b.value : str;
    })
    $.post("/StrokeCenter/MedicalRecords/SetPatinet_BaseInfor", { datajson: JSON.stringify(data), rnd: getRnd() }, function (data, textStatus) {
        if (textStatus == "success") {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                $.ligerDialog.success("保存成功", "", function () {
                    window.location.reload(true);
                });
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        }
    })
}

function delmr() {
    if (IsEmpty(patientId)) return;
    $.ligerDialog.confirm('确定删除该患者吗', function (yes) {
        if (yes) {
            var url = "/StrokeCenter/PatientList/GiveUp";
            $.get(url, {
                rnd: new Date().getMilliseconds(),
                patientId: patientId
            }, function (data, textStatus) {
                if (textStatus == "success") {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("操作成功", "", function () {
                            window.location.href = "/StrokeCenter/MedicalRecords/index";
                        });
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                }
            })
        }
    });
}

function position() {
    LiftEffect({
        "control1": ".lift-nav",
        "control2": ".lift",
        "target": [".t0", ".t1"],
        "current": "current"
    });
}

var FiledArray = [
    { name: "姓名", code: "txtPATIENT_NAME", tocode: "PATIENT_NAME", value: "", type: "text" },
    { name: "性别", code: "rdoPATIENT_SEX", tocode: "PATIENT_SEX", value: "", type: "radio" },
    { name: "身份证", code: "txtPATIENT_ID_NUM", tocode: "PATIENT_ID_NUM", value: "", type: "text" },
    { name: "发病时间", code: "txtEMS_DISEASE_TIME", tocode: "EMS_DISEASE_TIME", value: "", type: "date" },
    { name: "呼救时间", code: "txtHelpTime", tocode: "HelpTime", value: "", type: "date" },
    { name: "首次医疗接触时间", code: "txtContact_Time", tocode: "Contact_Time", value: "", type: "date" },
    { name: "来院方式", code: "rdoPATIENT_ARRIVE_CLASS", tocode: "PATIENT_ARRIVE_CLASS", value: "", type: "radio" },
    { name: "入院途径", code: "rdoAdmissionPath", tocode: "AdmissionPath", value: "", type: "radio" },
    { name: "到院时间", code: "txtEMS_ARRIVEDOOR_TIME", tocode: "EMS_ARRIVEDOOR_TIME", value: "", type: "date" },
    { name: "医疗付款方式", code: "rdoPatient_Pay_Type", tocode: "Patient_Pay_Type", value: "", type: "radio" },
    { name: "入院mRS评分", code: "rdoAssessment_MRS_Bool", tocode: "Assessment_MRS_Bool", value: "", type: "radio" },
    { name: "入院mRS评分", code: "txtAssessment_MRS_Score", tocode: "Assessment_MRS_Score", value: "", type: "text" },
    { name: "入院GCS评分", code: "rdoAssessment_GCS_Bool", tocode: "Assessment_GCS_Bool", value: "", type: "radio" },
    { name: "入院GCS评分", code: "txtAssessment_GCS_Score", tocode: "Assessment_GCS_Score", value: "", type: "text" },
    { name: "吞咽功能评估", code: "rdoAssessment_Swallowing_Bool", tocode: "Assessment_Swallowing_Bool", value: "", type: "radio" },
    { name: "洼田饮水实验", code: "txtAssessment_Swallowing_Content", tocode: "Assessment_Swallowing_Content", value: "", type: "text" },
    { name: "身高(cm)", code: "txtPatient_Height", tocode: "Patient_Height", value: "", type: "text" },
    { name: "体重(kg)", code: "txtPatient_Weight", tocode: "Patient_Weight", value: "", type: "text" },
    { name: "收缩压(mmHg)", code: "txtPatientBloodH", tocode: "PatientBloodH", value: "", type: "text" },
    { name: "舒张压(mmHg)", code: "txtPatientBloodL", tocode: "PatientBloodL", value: "", type: "text" },
    { name: "BMI(kg/㎡)", code: "txtPatient_BMI", tocode: "Patient_BMI", value: "", type: "text" },
    { name: "脉搏(次/分)", code: "txtPATIENT_PULSE", tocode: "PATIENT_PULSE", value: "", type: "text" },
    { name: "血氧饱和度(%)", code: "txtAssessment_Oxygen", tocode: "Assessment_Oxygen", value: "", type: "text" },
    { name: "呼吸(次/分)", code: "txtPATIENT_BREATHE", tocode: "PATIENT_BREATHE", value: "", type: "text" },
    { name: "体温(°)", code: "txtAssessment_BodyTemperature", tocode: "Assessment_BodyTemperature", value: "", type: "text" },
    { name: "意识", code: "chkPATIENT_CONSCIOUSNESS", tocode: "PATIENT_CONSCIOUSNESS", value: "", type: "checkbox" },
    { name: "主诉及症状", code: "txtAssessment_Illness", tocode: "Assessment_Illness", value: "", type: "text" }
];